//
//  UsePartView.m
//  HondaPro_2
//
//  Created by Eric on 13-6-3.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "UsePartView.h"
#import "MainMenuDataModel.h"
#import "UserDefinedCellForSubView2ForGetJobSheet.h"

@interface UsePartView ()

/** 收费区分列表 */
@property(nonatomic,strong)NSMutableDictionary *chargeDiff;

//添加控件
-(void)createStaticImg;
-(void)createLbl;
-(void)createTableView;

//刷新控件内容
-(void)freshenViewDis;

@end

@implementation UsePartView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createStaticImg];
        [self createTableView];
        [self createLbl];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    [self freshenViewDis];
}

#pragma  mark - 添加控件

-(void)createStaticImg
{
    CGRect frame = [self setupLabelWithTitle:@"收费区分" PreFrame:CGRectZero lableWidth:70];
    frame = [self setupLabelWithTitle:@"项目代码" PreFrame:frame lableWidth:100];
    frame = [self setupLabelWithTitle:@"项目名称" PreFrame:frame lableWidth:230];
    frame = [self setupLabelWithTitle:@"配件代码" PreFrame:frame lableWidth:80];
    frame = [self setupLabelWithTitle:@"备件名称" PreFrame:frame lableWidth:178];
    frame = [self setupLabelWithTitle:@"销售单价" PreFrame:frame lableWidth:80];
    frame = [self setupLabelWithTitle:@"数量" PreFrame:frame lableWidth:80];
    frame = [self setupLabelWithTitle:@"金额" PreFrame:frame lableWidth:80];
    frame = [self setupLabelWithTitle:@"折扣" PreFrame:frame lableWidth:50];
}

-(CGRect)setupLabelWithTitle:(NSString *)title PreFrame:(CGRect)preFrame lableWidth:(CGFloat)lblW{
    CGFloat lblY = 0;
    CGFloat lblH = 50;
    
    CGFloat lblX = CGRectGetMaxX(preFrame);
    UILabel *lbl = [[UILabel alloc] init];
    lbl.text = title;
    lbl.frame = CGRectMake(lblX, lblY, lblW, lblH);
    lbl.backgroundColor = [UIColor clearColor];
    lbl.textColor = [UIColor grayColor];
    lbl.font = [UIFont fontWithName:@"ArialMT" size:16];
    lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    lbl.numberOfLines = 2;
    lbl.textAlignment = NSTextAlignmentCenter;
    [self addSubview:lbl];
    return lbl.frame;
}

-(void)createLbl
{
    partChargeTotalLbl = [[UILabel alloc] initWithFrame:CGRectMake(600, 380, 300, 50)];
    partChargeTotalLbl.backgroundColor = [UIColor whiteColor];
    partChargeTotalLbl.font = [UIFont systemFontOfSize:15];
    partChargeTotalLbl.textColor = [UIColor darkGrayColor];
    [self addSubview:partChargeTotalLbl];
}

-(void)createTableView
{
    tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, 260)];
    tableViewDis.backgroundColor = [UIColor clearColor];
    
    tableViewDis.dataSource = self;
    tableViewDis.delegate = self;
    
    [self addSubview:tableViewDis];
}

- (NSString *)transString:(id)obj
{
    if ([obj isKindOfClass:[NSString class]]) {
        return obj;
    }
    
    NSNumber *newObj = (NSNumber *)obj;
    return newObj.stringValue;
}

- (NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    //nslog(@"%@",dic);
    return dic;
}

#pragma mark - 刷新控件内容

-(void)freshenViewDis
{
    [tableViewDis reloadData];
    
    double total = 0;
    for (NSDictionary *dic in self.usePartDic) {
        total += [dic[@"PART_SALES_AMOUNT"]doubleValue];
    }
    partChargeTotalLbl.text = [NSString stringWithFormat:@"零件总费用：%.2f",total];
}

#pragma mark - tableView DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6+[self.usePartDic count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UserDefinedCellForSubView2ForGetJobSheet *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForSubView2ForGetJobSheet alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    NSInteger row = indexPath.row;
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSArray *arrRepairItemList = modelIns.partList;
    cell.row = row;
    if (row<[arrRepairItemList count])
    {
        cell.data = arrRepairItemList[row];
    }
    else
    {
        cell.data = nil;
    }
    
    return cell;
}

#pragma mark - tableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

//创建一个静态lbl
-(UILabel *)createAnStaticLblByFrame:(CGRect)xingCanRect withTxt:(NSString *)strTxt andBackgroundColor:(UIColor *)bgColor
{
    UILabel *lbl = [[UILabel alloc] initWithFrame:xingCanRect];
    lbl.backgroundColor = bgColor;
    lbl.font = [UIFont systemFontOfSize:13];
    lbl.textColor = [UIColor darkGrayColor];
    lbl.text = strTxt;
    
    if ([strTxt isEqualToString:@"序号"])
    {
        lbl.textAlignment = NSTextAlignmentCenter;
    }
    
//    if (![strTxt isEqualToString:@""])
//    {
//        lbl.backgroundColor = [UIColor redColor];
//    }
    
    return lbl;
}

-(NSMutableDictionary *)chargeDiff{
    if (_chargeDiff == nil) {
        _chargeDiff = [NSMutableDictionary dictionary];
        NSMutableArray *arr = [self readershuDICimform][@"TM_CHARGE_PARTITION"];
        for (NSDictionary *dic in arr) {
            NSString *name = dic[@"NAME"];
            NSString *val = dic[@"CODE"];
            [_chargeDiff setObject:name forKey:val];
        }
    }
    return _chargeDiff;
}

@end
